﻿using Microsoft.AspNetCore.Mvc;
using StudentManagement.BO;
using StudentManagement.Entities;
using System;

namespace StudentManagement.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class TbcUserController : ControllerBase
    {
        private readonly TbcUserBO _userBO;

        public TbcUserController(TbcUserBO userBO)
        {
            _userBO = userBO;
        }

        /// <summary>
        /// 1. 单条记录查询
        /// </summary>
        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            try
            {
                var user = _userBO.GetUserById(id);
                if (user == null)
                    return NotFound(new { success = false, message = "用户不存在" });

                return Ok(new { success = true, data = user });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 2. 添加单条记录
        /// </summary>
        [HttpPost]
        public IActionResult Post([FromBody] TbcUserInfo user)
        {
            try
            {
                int userId = _userBO.AddUser(user);
                return Ok(new
                {
                    success = true,
                    message = "添加成功",
                    data = new { userId = userId }
                });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 3. 修改单条记录
        /// </summary>
        [HttpPut("{id}")]
        public IActionResult Put(int id, [FromBody] TbcUserInfo user)
        {
            try
            {
                user.TbcUserID = id; // 确保ID一致
                bool result = _userBO.UpdateUser(user);
                if (result)
                    return Ok(new { success = true, message = "更新成功" });
                else
                    return NotFound(new { success = false, message = "用户不存在或更新失败" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 4. 删除单条记录
        /// </summary>
        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            try
            {
                bool result = _userBO.DeleteUser(id);
                if (result)
                    return Ok(new { success = true, message = "删除成功" });
                else
                    return NotFound(new { success = false, message = "用户不存在" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 5. 分页与查询
        /// </summary>
        [HttpGet("paged")]
        public IActionResult GetPaged(string keyword = "", int pageIndex = 1, int pageSize = 10)
        {
            try
            {
                var result = _userBO.GetPagedUsers(keyword, pageIndex, pageSize);
                return Ok(new
                {
                    success = true,
                    data = result.Data,
                    totalCount = result.TotalCount,
                    pageIndex = pageIndex,
                    pageSize = pageSize,
                    totalPages = (int)Math.Ceiling(result.TotalCount / (double)pageSize)
                });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = ex.Message });
            }
        }
    }
}